package vip.xiaonuo.smzq.modular.kcl.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import vip.xiaonuo.smzq.modular.kcl.entity.Bg;
import vip.xiaonuo.smzq.modular.kcl.mapper.BgMapper;
import vip.xiaonuo.smzq.modular.kcl.service.IBgService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import vip.xiaonuo.common.page.CommonPageRequest;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 报告 服务实现类
 *
 * @author eomer
 * @since 2024-02-22
 */
@Service
public class BgServiceImpl extends ServiceImpl<BgMapper, Bg> implements IBgService {

    @Resource
    private BgMapper bgMapper;

    @Override
    public Page<Bg> page(Bg bg) {
        QueryWrapper<Bg> queryWrapper = new QueryWrapper<>();
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Override
    public List<Bg> getBgList(String kqbm, Integer djflbm, Integer nd) {
        List<Bg> bgList = new ArrayList<>();
        try {
            QueryWrapper<Bg> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("nd", nd).eq("kqbh", kqbm).eq("djflbh", djflbm);
            bgList=bgMapper.selectList(queryWrapper);
        }catch (Exception e){
            e.printStackTrace();
        }

        return bgList;
    }

    @Override
    public int addList(List<Bg> bgList,String kqbm, Integer djflbm, Integer nd) {
        int count = 0;
        for (Bg bg:bgList) {
            bg.setKqbh(kqbm);
            bg.setDjflbh(djflbm);
            bg.setNd(nd);
            if (bg.getPkid() != null && bg.getPkid().length() > 0){
                count+=  bgMapper.updateById(bg);
            }else {
                count+=  bgMapper.insert(bg);
            }
        }
        return count;
    }

    @Override
    public List<Bg> selectList(QueryWrapper<Bg> queryWrapper) {
        return bgMapper.selectList(queryWrapper);
    }

    @Override
    public void delete(String kqbh, Integer djflbh, Integer nd) {
        QueryWrapper<Bg> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("nd", nd).eq("kqbh", kqbh).eq("djflbh", djflbh);
        bgMapper.delete(queryWrapper);
    }
}
